package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HouseRepairDAO;
import com.foreveross.vo.Express;
import com.foreveross.vo.HouseRepair;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.common.PageVo;

@Component("houseRepairDAO")
public class HouseRepairDAOImp extends SQLUtil implements HouseRepairDAO {

	@Override
	public List<HouseRepair> findHouseRepairList(String repair_no, String name,
			String phone, String type,String typeName, String status,String houseId ,String resCode,int pageIndex,
			int pageSize,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from HouseRepair model where 1=1 ");
		if (!"".equals(repair_no) && null != repair_no) {
			queryString
					.append(" and model.repairNo like '%" + repair_no + "%'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.houseUser.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone  like '%" + phone + "%'");
		}

		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type='"+ type+"'");
		}
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and model.typeName like '%"+ typeName+"%'");
		}
		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status='"+ status+"'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.hrHouses.id='"+ houseId+"'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%" + resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId,  "hrHouses.id");
		queryString.append(" order by model.createTime desc");
		return (List<HouseRepair>) findObjectAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public long getHouseRepairCount(String repair_no, String name,
			String phone, String type,String typeName, String status,String houseId,String resCode,String userId) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from HouseRepair model where 1=1 ");
		if (!"".equals(repair_no) && null != repair_no) {
			queryString
					.append(" and model.repairNo like '%" + repair_no + "%'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.houseUser.name like '%" + name + "%'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString.append(" and model.phone  like '%" + phone + "%'");
		}

		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type='"+ type+"'");
		}
		if (!"".equals(typeName) && null != typeName) {
			queryString.append(" and model.typeName like '%"+ typeName+"%'");
		}
		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status='"+ status+"'");
		}
		
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.hrHouses.id='" + houseId+"'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%" + resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId, "hrHouses.id");
		queryString.append(" order by model.createTime desc");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}


	@Override
	public PageVo<HouseRepair> getMyHouseRepair(String weixinid,
			String currentPage, String pageSize) throws Exception {
		PageVo<HouseRepair> page = new PageVo<HouseRepair>();
		String sqlfrom = "select model ";
		StringBuffer queryString = new StringBuffer(
				" from HouseRepair model where 1=1 ");
		if (!"".equals(weixinid) && null != weixinid) {
			queryString.append(" and model.houseUser.wechatId = '" + weixinid
					+ "'");
		}
		queryString.append(" order by model.createTime desc");
		page.settotal(Integer.parseInt(findObjectAll(
				"select count(*)" + queryString.toString()).get(0).toString()));
		if (currentPage != null && pageSize != null) {
			page.init(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
			page.setData((List<HouseRepair>) findObjectAll(sqlfrom
					+ queryString.toString(), page.getpageIndex(),
					Integer.parseInt(pageSize)));
		} else
			page.setData((List<HouseRepair>) findObjectAll(sqlfrom
					+ queryString.toString()));
		return page;
	}

	@Override
	@Transactional
	public void updateHouserRepair(String id, String status) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append("update HouseRepair model set model.status ='"+status+"'");
		queryString.append(" where model.id = '"+id+"'");
		updateObejctByHql(queryString.toString());
	}

}
